package org.zjvis.datascience.service;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.zjvis.datascience.common.dto.ModelLocalDTO;
import org.zjvis.datascience.common.dto.ProjectDTO;
import org.zjvis.datascience.common.exception.DataScienceException;
import org.zjvis.datascience.common.util.CollectionUtil;
import org.zjvis.datascience.common.util.DozerUtil;
import org.zjvis.datascience.common.util.JwtUtil;
import org.zjvis.datascience.common.util.RestTemplateUtil;
import org.zjvis.datascience.common.vo.CheckModelVo;
import org.zjvis.datascience.service.mapper.CaseMapper;
import org.zjvis.datascience.service.mapper.ModelLocalMapper;
import org.zjvis.datascience.service.mapper.UserProjectMapper;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CommunityCaseService {

    @Lazy
    @Autowired
    ProjectService projectService;

    @Autowired
    CaseMapper caseMapper;

    @Autowired
    ModelLocalMapper modelLocalMapper;

    @Autowired
    RestTemplateUtil restTemplateUtil;

    @Autowired
    UserProjectMapper userProjectMapper;


    /**
     * 案例安装
     */
    public void caseInstall(int caseId) {

        long userId = JwtUtil.getCurrentUserId();

        Long projectId = getProjectId(caseId);

        ProjectDTO projectDTO = projectService.getById(projectId);

        if (ObjectUtil.isNotEmpty(projectDTO)) {

            projectService.caseInstall(projectId);

        } else {

            throw new DataScienceException("案例项目信息为空！");
        }


    }

    /**
     * 获取case  proID
     *
     * @param caseId
     * @return
     */
    public Long getProjectId(int caseId) {

        JSONObject params = new JSONObject();

        params.put("caseId", caseId);

        Object result = restTemplateUtil.submitMOJob(params, "getProjectId").getResult();

        if (ObjectUtil.isEmpty(result)) {

            throw new DataScienceException("该案例不存在模板项目!");

        }
        JSONObject object = JSONObject.parseObject(JSONObject.toJSONString(result));


        Integer projectId = (Integer) object.get("projectId");

        return projectId.longValue();


    }

    /**
     * 更新case -projectId
     *
     * @param caseId
     * @param projectId
     */
    public void setProjectId(int caseId, Long projectId) {

        JSONObject params = new JSONObject();

        params.put("caseId", caseId);
        params.put("projectId", projectId);

        Object result = restTemplateUtil.submitMOJob(params, "setProjectId").getResult();


    }

    /**
     * 校验模型
     *
     * @param caseId
     * @return
     */
    public List<CheckModelVo> checkModel(int caseId) {

        //获取案例项目相关信息
        Long projectId = getProjectId(caseId);

        Long caseUserId = userProjectMapper.getUserId(projectId);

        List<ModelLocalDTO> caseList = modelLocalMapper.queryByProjectId(caseUserId, projectId);

        List<Long> modelId = caseList.stream().map(ModelLocalDTO::getModelId).distinct().collect(Collectors.toList());


        //获取当前用户 已安装模型
        long userId = JwtUtil.getCurrentUserId();


        List<ModelLocalDTO> userList = modelLocalMapper.queryUserLocal(userId);

        List<Long> userModelId = userList.stream().map(ModelLocalDTO::getModelId).distinct().collect(Collectors.toList());
        //取差集

        modelId.removeAll(userModelId);

        if (CollectionUtil.isEmpty(modelId)) {

            return null;
        }


        List<CheckModelVo> list = new LinkedList();

        //获取差集dto并封装
        modelId.forEach(e -> {

            JSONObject params = new JSONObject();
            params.put("modelId", e);
            params.put("userId", caseUserId);

            List<ModelLocalDTO> localDTOS = modelLocalMapper.queryByModelId(params);

            localDTOS.forEach(f -> {

                CheckModelVo modelVo = DozerUtil.mapper(f, CheckModelVo.class);

                list.add(modelVo);
            });


        });

        return list;


    }

    public LinkedList<CheckModelVo> getModels(int caseId) {

        //获取案例项目相关信息
        Long projectId = getProjectId(caseId);

        Long caseUserId = userProjectMapper.getUserId(projectId);

        List<ModelLocalDTO> caseList = modelLocalMapper.queryByProjectId(caseUserId, projectId);

        LinkedList<CheckModelVo> list = new LinkedList<>();

        caseList.forEach(e -> {

            CheckModelVo vo = DozerUtil.mapper(e, CheckModelVo.class);

            list.add(vo);

        });

        return list;
    }
}
